﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using Maticsoft.Common;

namespace WEB.T_bill
{
    public partial class xiaoZhang2 : System.Web.UI.Page
    {
        CustomsSystem.BLL.BillManage billBLL = new BillManage();
        CustomsSystem.Model.Bill billModel = new CustomsSystem.Model.Bill();
        CustomsSystem.BLL.PayManage payBLL = new PayManage();
        CustomsSystem.Model.Pay payModel = new CustomsSystem.Model.Pay();
        int num = 0;//用此值来标志被勾选的委托单有几个
        int check = 0;//用此值来标志是否有委托单被勾选


        CustomsSystem.BLL.SheetInfoManage sheetInfoBLL = new SheetInfoManage();
        public DataSet ds;
        int billTypeID = 0;
        string userName = "";
        int sheetID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
            this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
            this.btnXiaoZhangYS.Attributes.Add("onClick", "OpenChildWinGZ()");
            this.btnXiaoZhangYF.Attributes.Add("onClick", "OpenChildWinGZ()");

            if (!Page.IsPostBack)
            {
                //默认时间为最近一月
                DateTime A = new DateTime();
                A = DateTime.Today;
                tbTimestop.Text = A.ToShortDateString();
                tbTimestart.Text = A.AddDays(-30).ToShortDateString();
                Bind();
            }
        }

        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Session["sheetID"] = gvSheetInfo.Rows[e.NewSelectedIndex].Cells[0].Text.ToString();
            Session["CustomerName"] = gvSheetInfo.Rows[e.NewSelectedIndex].Cells[4].Text.ToString();

            int id = Convert.ToInt32(Request.QueryString["id"]);
            switch (id)
            {
                case 1: Response.Redirect("cost.aspx"); break;
                case 2: Response.Redirect("payment.aspx"); break;
                case 3: Response.Redirect("actualReceivables.aspx"); break;
                case 4: Response.Redirect("actualPayment.aspx"); break;
                default: return;
            }
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();

        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Bind()
        {
            SheetInfoManage sheetManage = new SheetInfoManage();

            string strQuery = ddlQuery.SelectedValue;
            string strSql = "  1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                if (ddlQuery.SelectedIndex == 8)
                {
                    if (PageValidate.IsNumber(tbQuery.Text.Trim()))
                    {
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                    }
                    else { MessageBox.Show(this, "请输入数字"); }
                }
                else
                {
                    strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());

                }
            }

            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + tbTimestop.Text.Trim() + "'";
                }
            }
            strSql = strSql + " order by sheetID " + ddlorder.SelectedValue; ;
            DataSet ds = new DataSet();
            ds = sheetManage.GetViewOfUnaudit(strSql);
            if (ds.Tables[0].Rows.Count < 1)
            { lbj.Text = "在该段时间内没有数据"; }
            else
            { lbj.Text = ""; }
            gvSheetInfo.DataSource = ds;
            gvSheetInfo.DataBind();
        }

        /// <summary>
        /// 排序同样绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlorder_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }


        /// <summary>
        /// 允许分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            //换页功能
            this.gvSheetInfo.PageIndex = e.NewPageIndex;
            //绑定GridView
            gvSheetInfo.DataBind();
            TextBox tb = (TextBox)gvSheetInfo.BottomPagerRow.FindControl("tbinPageNum");
            tb.Text = (gvSheetInfo.PageIndex + 1).ToString();
            Bind();
        }




        /// <summary>
        /// 跳页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                gvSheetInfo.DataBind();

                TextBox tb = (TextBox)gvSheetInfo.BottomPagerRow.FindControl("tbinPageNum");
                int num = Int32.Parse(tb.Text.Trim());

                GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                gvSheetInfo_PageIndexChanging1(null, ea);
            }
            catch { MessageBox.Show(this, "请输入数字页码"); }
        }


        /// <summary>
        /// 应付销账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXiaoZhangYF_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvSheetInfo.Rows.Count; i++)
            {
                CheckBox cb = this.gvSheetInfo.Rows[i].FindControl("cbxXiaoZhang") as CheckBox;
                if (cb.Checked)
                {
                    
                    DataSet ds = new DataSet();//获取费用表中委托单的billID
                    sheetID = Convert.ToInt32(this.gvSheetInfo.Rows[i].Cells[0].Text);

                    //判断该委托单是否有应付费用
                    string feiYong = billBLL.GetSfeeAmount(sheetID, 1).ToString();
                    if (feiYong == "")
                    {
                        MessageBox.Show(this, "没有应付费用");
                    }
                    else
                    {
                        num += 1;
                        billTypeID = 2;
                        ds = billBLL.getBillID(sheetID, billTypeID);

                        for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                        {
                            int billID = Convert.ToInt32(ds.Tables[0].Rows[j][0]);//获得T_bill表中的billID号
                            billModel = billBLL.GetModel(billID);
                            //把T_bill中的数据存到T_pay表中
                            payModel.billID = billModel.billID;
                            payModel.sheetID = billModel.sheetID;
                            payModel.cID = billModel.cID;
                            payModel.feeTypeID = billModel.feeTypeID;
                            payModel.sfeeAmount = billModel.sfeeAmount;

                            payModel.payTypeID = 2;

                            //根据用户名去Cu_User里拿相应的用户ID
                            userName = Session["UserName"].ToString();
                            CustomsSystem.BLL.UserManage userBLL = new CustomsSystem.BLL.UserManage();
                            string empNum = userBLL.GetSingle(userName).ToString();
                            payModel.empID = empNum;

                            payModel.payDate = DateTime.Now;
                            payModel.payType = 0;

                            int result = payBLL.Add(payModel);
                        }
                        billBLL.updatePayedID(sheetID, billTypeID);

                        //把挂账单位录进T_sheetInfo表中
                        string guaZhang = Session["guaZhang"].ToString();
                        sheetInfoBLL.AddGuaZhangDW(sheetID, guaZhang);
                    }
                }
            }
            for (int i = 0; i < gvSheetInfo.Rows.Count; i++)
            {
                string sheetID = this.gvSheetInfo.Rows[i].Cells[0].Text.ToString();
                string payTypeID = payBLL.GetPayTypeID(sheetID).ToString();//根据sheetID去T_pay表里获取payTpyeID,1表示只有应收，2表示只有应付
                string payTypeNum = payBLL.GetPayTypeNum(sheetID).ToString();//根据sheetID去T_pay表里获取应收、应付的销账情况，2为全都销了
                if (payTypeNum == "2")
                {
                    this.gvSheetInfo.Rows[i].Visible = false;
                }
                if (payTypeNum == "1")
                {
                    if (payTypeID == "1")
                    {
                        this.gvSheetInfo.Rows[i].BackColor = System.Drawing.Color.GreenYellow;
                    }
                    if (payTypeID == "2")
                    {
                        this.gvSheetInfo.Rows[i].BackColor = System.Drawing.Color.SkyBlue;
                    }
                }
            }
            if (num == 1)
            {
                MessageBox.Show(this, "应付账款全部销账成功");
            }
            else if (num != 0 && num != 1)
            {
                MessageBox.Show(this, "应付账款全部销账成功");
            }
            else
            {
                if (check == 0)
                {
                    MessageBox.Show(this, "您还没有勾选需要销账的委托单！");
                }
            }
        }

        /// <summary>
        /// 应收销账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXiaoZhangYS_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvSheetInfo.Rows.Count; i++)
            {
                CheckBox cb = this.gvSheetInfo.Rows[i].FindControl("cbxXiaoZhang") as CheckBox;
                if (cb.Checked)
                {
                    check += 1;
                    DataSet ds = new DataSet();//获取费用表中委托单的billID
                    sheetID = Convert.ToInt32(this.gvSheetInfo.Rows[i].Cells[0].Text);

                    //判断该委托单是否有应收费用
                    string feiYong = billBLL.GetSfeeAmount(sheetID, 1).ToString();
                    if (feiYong == "")
                    {
                        MessageBox.Show(this, "没有应收费用");
                    }
                    else
                    {
                        num += 1;
                        billTypeID = 1;

                        ds = billBLL.getBillID(sheetID, billTypeID);

                        for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                        {
                            int billID = Convert.ToInt32(ds.Tables[0].Rows[j][0]);//获得T_bill表中的billID号
                            billModel = billBLL.GetModel(billID);
                            //把T_bill中的数据存到T_pay表中
                            payModel.billID = billModel.billID;
                            payModel.sheetID = billModel.sheetID;
                            payModel.cID = billModel.cID;
                            payModel.feeTypeID = billModel.feeTypeID;
                            payModel.sfeeAmount = billModel.sfeeAmount;

                            payModel.payTypeID = 1;

                            //根据用户名去Cu_User里拿相应的用户ID
                            userName = Session["UserName"].ToString();
                            CustomsSystem.BLL.UserManage userBLL = new CustomsSystem.BLL.UserManage();
                            string empNum = userBLL.GetSingle(userName).ToString();
                            payModel.empID = empNum;

                            payModel.payDate = DateTime.Now;
                            payModel.payType = 0;

                            int result = payBLL.Add(payModel);
                        }
                        billBLL.updatePayedID(sheetID, billTypeID);//在T_bill表中把payedID改为1，表示已销账

                        //把挂账单位录进T_sheetInfo表中
                        string guaZhang = Session["guaZhang"].ToString();
                        sheetInfoBLL.AddGuaZhangDW(sheetID, guaZhang);
                    }
                }
            }
            for (int i = 0; i < gvSheetInfo.Rows.Count; i++)
            {
                string sheetID = this.gvSheetInfo.Rows[i].Cells[0].Text.ToString();
                string payTypeID = payBLL.GetPayTypeID(sheetID).ToString();//根据sheetID去T_pay表里获取payTpyeID,1表示只有应收，2表示只有应付
                string payTypeNum = payBLL.GetPayTypeNum(sheetID).ToString();//根据sheetID去T_pay表里获取应收、应付的销账情况，2为全都销了
                if (payTypeNum == "2")
                {
                    this.gvSheetInfo.Rows[i].Visible = false;
                }
                if (payTypeNum == "1")
                {
                    if (payTypeID == "1")
                    {
                        this.gvSheetInfo.Rows[i].BackColor = System.Drawing.Color.GreenYellow;
                    }
                    if (payTypeID == "2")
                    {
                        this.gvSheetInfo.Rows[i].BackColor = System.Drawing.Color.SkyBlue;
                    }
                }
            }
            if (num == 1)
            {
                MessageBox.Show(this, "应收账款销账成功");
            }
            else if (num != 0 && num != 1)
            {
                MessageBox.Show(this, "应收账款全部销账成功");
            }
            else
            {
                if (check == 0)
                {
                    MessageBox.Show(this, "您还没有勾选需要销账的委托单!");
                }
            }
        }

        /// <summary>
        /// 控制行的颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            CustomsSystem.BLL.BillManage billBLL = new BillManage();
            CustomsSystem.BLL.PayManage payBll = new PayManage();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i <= gvSheetInfo.Rows.Count; i++)
                {
                    string sheetID = e.Row.Cells[0].Text;
                    string payTypeID = payBll.GetPayTypeID(sheetID).ToString();//根据sheetID去T_pay表里获取payTpyeID,1表示只有应收，2表示只有应付
                    string payTypeNum = payBll.GetPayTypeNum(sheetID).ToString();//根据sheetID去T_pay表里获取应收、应付的销账情况，2为全都销了

                    if (payTypeNum == "2")
                    {
                        e.Row.Visible = false;
                    }
                    if (payTypeNum == "1")
                    {
                        if (payTypeID == "1")
                        {
                            e.Row.BackColor = System.Drawing.Color.GreenYellow;
                        }
                        if (payTypeID == "2")
                        {
                            e.Row.BackColor = System.Drawing.Color.SkyBlue;
                        }
                    }
                }
            }
        }
    }
}